<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="岗位分类" prop="jobClass"
                :rules="[
                 ]">
            <!-- <el-input v-model="inputForm.jobClass" placeholder="请填写岗位分类"     ></el-input> -->
            <el-select v-model="inputForm.jobClass" placeholder="请填写岗位分类" style="width:100%;" >
              <el-option v-for="item in jobArr" :label="item.jobClass" :value="item.jobClass" :key="item.jobClass"></el-option>
            </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="岗位性质" prop="jobNature"
                :rules="[
                 ]">
              <el-input v-model="inputForm.jobNature" placeholder="请填写岗位性质"   disabled=""  ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="岗位现状" prop="jobStatus"
                :rules="[
                 ]">
              <el-input v-model="inputForm.jobStatus" placeholder="请填写岗位现状"   disabled=""  ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        jobArr: [],
        inputForm: {
          id: '',
          jobNature: '',
          jobClass: '',
          jobStatus: '',
          sort: '',
          remarks: ''
        }
      }
    },
    components: {
    },
    watch: {
      'inputForm.jobClass': {
        handler (newVal) {
          for (let i in this.jobArr) {
            if (this.jobArr[i].jobClass === newVal) {
              this.inputForm.jobNature = this.jobArr[i].jobNature
              this.inputForm.jobStatus = this.jobArr[i].jobStatus
              break
            }
          }
        },
        immediate: true,
        deep: false
      }
    },
    methods: {
      init (method, obj) {
        this.method = method
        this.jobGetData()
        if (method === 'add') {
          this.title = `新建人员岗位`
        } else if (method === 'edit') {
          this.title = '修改人员岗位'
        } else if (method === 'view') {
          this.title = '查看人员岗位'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
          }
        })
      },
      jobGetData () {
        this.jobArr = []
        this.$http({
          url: '/basicdata/basicjob/basicJob/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 9999
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.jobArr = data.page.list
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>
